{  Statement: http://acmp.ru/?main=task&id_task=446
   Verdict: Accepted
}
uses
    SysUtils, Math;

const
    a:array[0..7] of string = ('.', '.B', '.G', '.GB', '.R', '.RB', '.RG', '.RGB');

var
    i,j,n,m,k:longint;
    b:array[0..110,0..110] of char;

begin
reset(input,'input.txt');
rewrite(output,'output.txt');
readln(n,m);
for i:=1 to n do begin
    for j:=1 to m do 
        read(b[i][j]);
    readln;
end;
for i := 1 to n do
    for j := 1 to m do begin
        read(k);
        if pos(b[i][j], a[k]) = 0 then begin
            write('NO');
            halt(0);
        end;
    end;
write('YES');
end.
